home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / iqb9104.zip / CLONE.BAS next >
BASIC Source File  |  1991-03-11  |  8KB  |  273 lines

  1. '---- Program:  CLONE.BAS
  2. '     Demonstrate cloning data into .EXE's.  Data is further
  3. '     protected via simple encryption technique and by checksum.
  4.  
  5. DEFINT A-Z
  6.  
  7. DECLARE FUNCTION Checksum% (TestString$)
  8. DECLARE FUNCTION CombineBytes% (HiByte%, LowByte%)
  9. DECLARE FUNCTION Cypher$ (dat$)
  10. DECLARE SUB CloneData (dat$, Program$)
  11. DECLARE FUNCTION GetClonedData$ (ProgName$, CloneErr%)
  12. DECLARE SUB HiLowBytes (IntValue%, HiByte%, LowByte%)
  13. DECLARE FUNCTION SearchEXE$ (ProgName$, Marker$)
  14. DECLARE SUB Uncypher (dat$)
  15.  
  16. DIM SHARED BytesToData&, DataLen AS INTEGER
  17.  
  18. '---- The copyright notice is for illustration purposes only.  You
  19. '     are allowed to use/modify this code to suit your needs.  -LS
  20. Copyright$ = "Copyright (C) 1990, Cobb Subscriber.  All Rights Relaxed."
  21. CLS
  22.  
  23. '---- Discover the checksum value then, REMark this line.
  24. 'PRINT Checksum%(Copyright$)
  25.  
  26. '---- If a hacker has tampered with the copyright notice then END.
  27. '     Note:  The value, 4657, was determined from the line above.
  28.  
  29. IF NOT Checksum%(Copyright$) = 4657 THEN
  30.     PRINT "The Copyright Notice is Altered - Program Terminated!"
  31.     END
  32. END IF
  33.  
  34. ProgName$ = "CLONE.EXE"
  35. YourName$ = GetClonedData$(ProgName$, CloneErr)
  36.  
  37. IF LEN(YourName$) = 0 AND NOT CloneErr THEN
  38.     PRINT "This Program Must Be Initialized With Your Name."
  39.  
  40.     INPUT "Please enter your name: ", YourName$
  41.  
  42.     '---- Set length <= DataLen - 1 and terminate with a null.
  43.     YourName$ = RTRIM$(LEFT$(YourName$, DataLen - 1)) + CHR$(0)
  44.  
  45.     CloneData YourName$, ProgName$
  46.     PRINT : PRINT "You Now Have Access Privilages."
  47.  
  48. ELSEIF CloneErr THEN
  49.     PRINT "The Program Data Area is Altered - Program Terminated!"
  50.     END
  51.  
  52. ELSE
  53.     INPUT "Please enter your name: ", NameCheck$
  54.     IF LEN(NameCheck$) > 29 THEN NameCheck$ = LEFT$(NameCheck$, 29)
  55.  
  56.     IF NameCheck$ <> RTRIM$(YourName$) THEN
  57.         PRINT "Unauthorized Access Denied!  Program Terminated!"
  58.         END
  59.     ELSE
  60.         PRINT "Welcome back, "; YourName$; "."
  61.     END IF
  62. END IF
  63.  
  64. FUNCTION Checksum% (TestString$) STATIC
  65.  
  66.     DEFINT A-Z
  67.  
  68.     test = 0
  69.     LoopCount = test
  70.  
  71.     FOR N = 1 TO LEN(TestString$)
  72.         LoopCount = N
  73.         IF N MOD 2 THEN
  74.             HiByte = ASC(MID$(TestString$, N, 1))
  75.         ELSE
  76.             GOSUB AddLowByte
  77.         END IF
  78.     NEXT
  79.  
  80.     '---- If the byte count is odd then we need to XOR
  81.     IF LoopCount MOD 2 THEN GOSUB AddLowByte
  82.     
  83.     Checksum% = test
  84.     EXIT FUNCTION
  85.  
  86. AddLowByte:
  87.     LowByte = ASC(MID$(TestString$, LoopCount, 1))
  88.     TwoBytes = CombineBytes(HiByte, LowByte)
  89.     test = test XOR TwoBytes
  90. RETURN
  91.  
  92. END FUNCTION
  93.  
  94. SUB CloneData (dat$, Program$)
  95.  
  96.     DEFINT A-Z
  97.     
  98.     '---- Obtain a checksum value for dat$ string.
  99.     checkS = Checksum%(dat$)
  100.  
  101.     '---- Break the checksum value into high and low bytes.
  102.     HiLowBytes checkS, HiByte, LowByte
  103.     
  104.     '---- Convert High and Low Bytes into characters.
  105.     CheckDat$ = CHR$(HiByte) + CHR$(LowByte)
  106.  
  107.     '---- Concatenate the strings for writing back to the EXE.
  108.     temp$ = LEFT$(dat$ + STRING$(DataLen, 0), DataLen) + CheckDat$
  109.  
  110.     temp$ = Cypher$(temp$)
  111.     
  112.     Handle = FREEFILE       'Get a number for the file.
  113.     OPEN Program$ FOR BINARY AS #Handle
  114.     PUT #Handle, BytesToData&, temp$
  115.     CLOSE #Handle
  116.  
  117. END SUB
  118.  
  119. FUNCTION CombineBytes% (HiByte%, LowByte%) STATIC
  120.  
  121.     DEFINT A-Z
  122.     '---- Combine the two bytes into one, long integer.
  123.     N& = (HiByte + 256&) + (LowByte * 256&) - 256
  124.  
  125.     '---- Adjust integer's range to signed short integer range.
  126.     IF N& > 32767 THEN N& = N& - 65536
  127.     
  128.     CombineBytes% = N&
  129.  
  130. END FUNCTION
  131.  
  132. FUNCTION Cypher$ (dat$)
  133.     DEFINT A-Z
  134.  
  135.     FOR N = 1 TO LEN(dat$)
  136.         '---- Test the character so it doesn't go greater than 255
  137.         test = ASC(MID$(dat$, N, 1)) + 100
  138.         IF test > 255 THEN test = test - 100
  139.  
  140.         '---- Build the cyphered string.
  141.         temp$ = temp$ + CHR$(test)
  142.     NEXT
  143.  
  144.     '---- Set the function with results.  We don't alter dat$
  145.     Cypher$ = temp$
  146.  
  147. END FUNCTION
  148.  
  149. FUNCTION GetClonedData$ (ProgName$, CloneErr%)
  150.  
  151.     DEFINT A-Z
  152.  
  153.     '---- Assign a token area big enough for marker, clone data
  154.     '     and a stored checksum value.
  155.     SpotForMarker$ = "ûà√Ö12345678901234567890123456789012"
  156.     TempMarker$ = LEFT$(SpotForMarker$, 4)
  157.     CloneErr = 0
  158.  
  159.     '---- Subtracting 4 bytes accounts for TempMarker$
  160.     DataLen = LEN(SpotForMarker$) - 4
  161.  
  162.     '---- Search the EXE for the cloned data.
  163.     dat$ = SearchEXE$(ProgName$, TempMarker$)
  164.     Uncypher dat$                 'Uncypher the data.
  165.  
  166.     '---- If we have a null then the clone area contains data.
  167.     Null = INSTR(dat$, CHR$(0))
  168.     IF Null THEN                  'We have data!
  169.  
  170.         '---- What is the stored checksum value?
  171.         HiByte = ASC(MID$(dat$, LEN(dat$) - 1))
  172.         LowByte = ASC(RIGHT$(dat$, 1))
  173.         Check = CombineBytes(HiByte, LowByte)
  174.  
  175.         '---- Compare the stored checksum to actual checksum.
  176.         IF Check = Checksum%(LEFT$(dat$, INSTR(dat$, CHR$(0)))) THEN
  177.             '---- Stored checksum matches actual checksum.
  178.             GetClonedData$ = LEFT$(dat$, (Null - 1))
  179.         ELSE
  180.             '---- A mad hacker has modified our cloned data.
  181.             CloneErr = -1
  182.             GetClonedData$ = ""
  183.         END IF
  184.     ELSE                          'We have no cloned data.
  185.         GetClonedData$ = ""
  186.     END IF
  187.  
  188.     '---- Subtracting 2 bytes accounts for checksum value.
  189.     DataLen = DataLen - 2
  190.  
  191. END FUNCTION
  192.  
  193. SUB HiLowBytes (IntValue%, HiByte%, LowByte%) STATIC
  194.  
  195.     DEFINT A-Z
  196.     
  197.     HiByte = IntValue AND &HFF
  198.     LowByte = IntValue \ &HFF
  199.  
  200. END SUB
  201.  
  202. FUNCTION SearchEXE$ (ProgName$, Marker$)
  203.  
  204.     DEFINT A-Z
  205.  
  206.     Bytes = 16000           '16000 bytes to read with each pass.
  207.     BytesToData& = 0        'Initialize it to zero.
  208.     SeekPos& = 1            'Initialize to file's 1st byte.
  209.     CountToMarker = 0       'The count to Marker$ location.
  210.  
  211.     Handle = FREEFILE       'Get a number for the file.
  212.     OPEN ProgName$ FOR BINARY AS #Handle
  213.     FiSize& = LOF(Handle)   'How big is the file?
  214.  
  215.     DO
  216.         '---- 16000 bytes is to many so, reduce it.
  217.         IF Bytes > FiSize& THEN Bytes = FiSize&
  218.  
  219.         '---- If SeekPos& + bytes is greater than FiSize&, reduce Bytes.
  220.         IF Bytes + SeekPos& > FiSize& THEN Bytes = FiSize& - SeekPos&
  221.  
  222.         '---- Point into the file (SeekPos&) and input "Bytes".
  223.         SEEK #Handle, SeekPos&
  224.         A$ = INPUT$(Bytes, Handle)
  225.  
  226.         CountToMarker = INSTR(A$, Marker$)  'Is Marker in A$ ?
  227.  
  228.         IF CountToMarker THEN               'Yes, we found the Marker!
  229.             '---- Calculate were the data is located.
  230.             BytesToData& = SeekPos& + CountToMarker + LEN(Marker$) - 1
  231.  
  232.             SEEK #Handle, BytesToData&      'Now get the data.
  233.             SearchEXE$ = INPUT$(DataLen, Handle)
  234.             CLOSE #Handle
  235.             EXIT FUNCTION                   'We're out of here!
  236.         END IF
  237.  
  238.         '---- We've reached the end of the file and did not find
  239.         '     our Marker$.  This prevents an endless loop.
  240.         IF Bytes + SeekPos& >= FiSize& THEN EXIT DO
  241.  
  242.         '---- We subtract the Marker length from our next SeekPos&
  243.         '     in case it's 4 bytes were split during the last input.
  244.         SeekPos& = Bytes + SeekPos& - LEN(Marker$)
  245.  
  246.     LOOP
  247.     
  248.     '---- The EXE has no Marker or, maybe, some hacker changed it.
  249.     CLOSE #Handle
  250.     PRINT "Error.  Could not find Marker!  Program Terminated!"
  251.     END
  252.     
  253. END FUNCTION
  254.  
  255. SUB Uncypher (dat$)
  256.     DEFINT A-Z
  257.  
  258.     FOR N = 1 TO LEN(dat$)
  259.         '---- Test whether dat$ is original, un-cyphered SpotForMarker$
  260.         test = ASC(MID$(dat$, N, 1)) - 100
  261.  
  262.         '---- If the character wasn't cyphered then don't uncypher it.
  263.         IF test < 0 THEN
  264.             temp$ = temp$ + CHR$(test + 100)
  265.         ELSE
  266.             temp$ = temp$ + CHR$(test)
  267.         END IF
  268.     NEXT
  269.  
  270.     SWAP dat$, temp$
  271. END SUB
  272.  
  273.